package com.dongdong.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.dongdong.data.Page;
import com.dongdong.mapper.TbSpecificationMapper;
import com.dongdong.mapper.TbSpecificationOptionMapper;
import com.dongdong.pojo.TbSpecification;
import com.dongdong.pojo.TbSpecificationExample;
import com.dongdong.pojo.TbSpecificationOption;
import com.dongdong.pojo.TbSpecificationOptionExample;
import com.dongdong.service.SpecificationService;
import com.dongdong.vo.SpecVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

@Service
@org.springframework.stereotype.Service
public class SpecificationServiceImpl implements SpecificationService {
    @Autowired
    private TbSpecificationMapper tbSpecificationMapper;
    @Autowired
    private TbSpecificationOptionMapper tbSpecificationOptionMapper;

    @Override
    public Page<TbSpecification> selectInfo(Integer pageNum, Integer pageSize, String specName) {
        //模糊查询分页
        PageHelper.startPage(pageNum,pageSize);
        List<TbSpecification> tbSpecifications = tbSpecificationMapper.selectByName(specName);
        PageInfo<TbSpecification> info = new PageInfo<>(tbSpecifications);
        return new Page(info.getPageNum(),info.getPageSize(),info.getTotal(),info.getPages(),info.getList());
    }

    @Override
    @Transactional
    public int deletebyIds(Long[] ids) {
        //先根据id删除规格表
        TbSpecificationExample tf = new TbSpecificationExample();
        TbSpecificationExample.Criteria criteria = tf.createCriteria();
        criteria.andIdIn(Arrays.asList(ids));
        int ii = tbSpecificationMapper.deleteByExample(tf);
        //再删除规格型号
        for (int i = 0; i < ids.length; i++) {
            TbSpecificationOptionExample te = new TbSpecificationOptionExample();
            TbSpecificationOptionExample.Criteria cc = te.createCriteria();
            cc.andSpecIdEqualTo(ids[i].longValue());
            tbSpecificationOptionMapper.deleteByExample(te);
        }
        return ii;
    }


   //增加
    @Override
    public int increaseInfo(TbSpecification tbSpecification, List<TbSpecificationOption> list) {
        //增加主表并返回id
        int i = tbSpecificationMapper.insert(tbSpecification);
        //遍历集合并增加从表
        for (TbSpecificationOption tbSpecificationOption:list) {
                tbSpecificationOption.setSpecId(tbSpecification.getId());
            int i2 = tbSpecificationOptionMapper.insert(tbSpecificationOption);
        }
        return i;
    }
    //回显规格信息
    @Override
    public SpecVO selectById(Long id) {
        //创建vo对象
        SpecVO vo = new SpecVO();
        //根据id查询规格
        TbSpecification tbSpecification = tbSpecificationMapper.selectByPrimaryKey(id);
        vo.setTbSpecification(tbSpecification);
        //根据id查询规格和排序
        TbSpecificationOptionExample to = new TbSpecificationOptionExample();
        to.createCriteria().andSpecIdEqualTo(id);
        List<TbSpecificationOption> optionList = tbSpecificationOptionMapper.selectByExample(to);
        vo.setOptionList(optionList);
        return vo;
    }
    //修改规格信息
    @Override
    public int updateSpecifiction(TbSpecification tbSpecification, List<TbSpecificationOption> optionList) {
        //先修改规格表
        tbSpecificationMapper.updateByPrimaryKeySelective(tbSpecification);
        //先删除tb_specification_option
        TbSpecificationOptionExample tse = new TbSpecificationOptionExample();
        tse.createCriteria().andSpecIdEqualTo(tbSpecification.getId());
       int i =  tbSpecificationOptionMapper.deleteByExample(tse);
        //再进行批量增加规格表
        for (TbSpecificationOption tbSpecificationOption:optionList) {
            tbSpecificationOption.setSpecId(tbSpecification.getId());
            tbSpecificationOptionMapper.insertSelective(tbSpecificationOption);
        }
        return i;
    }

    @Override
    public List<TbSpecification> selectAll() {
        return tbSpecificationMapper.selectByExample(null);
    }
}
